package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.AdvertDTO;
import com.financing.platform.service.IAdvertService;
import com.financing.platform.util.DTOdateSet;

/**
 * 
* @ClassName: AdvertAction 
* @Description: 广告 控制层
* @author lisu
*
 */
@Controller("advertActionAPI")
@RequestMapping("/api/advert")
@Scope("prototype")
public class AdvertAction extends BaseAction {

	@Autowired
	private IAdvertService advertServiceImpl;
	
	/**
	 * 
	* @Title: addAdvert 
	* @Description: 添加广告
	* @param   request
	* @param   response
	* @param   advertDTO
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/addAdvert.shtml")
	@ResponseBody
	public ResultMap addAdvert(HttpServletRequest request,
			HttpServletResponse response,AdvertDTO advertDTO) {
		
		log.info("In AdvertAction addAdvert Begin!");
		
		try{
			DTOdateSet.setUpdateInfo(advertDTO, request.getSession());
			DTOdateSet.setCreateInfo(advertDTO, request.getSession());
			advertServiceImpl.addAdvert(advertDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("AdvertAction addAdvert error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In AdvertAction addAdvert end!");	
		
		return resultMap;
	}
	
  /**
	* 
	* @Title: queryAdvertList 
	* @Description: 查询广告列表
	* @param   request
	* @param   response
	* @param   advertDTO
	* @return ResultMap   
	* @throws
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/queryAdvertList.shtml")
	@ResponseBody
	public ResultMap queryAdvertList(HttpServletRequest request,
			HttpServletResponse response,AdvertDTO advertDTO,Pager pager) {
		
		log.info("In AdvertAction queryAdvertList Begin!");
		
		try{
			advertDTO.setBegin(pager.getStartIndex());
			advertDTO.setPageSize(pager.getPageSize());
			
			int count = advertServiceImpl.queryAdvertListCount(advertDTO);
			pager.setTotalCount(count);
			
			List<AdvertDTO> list;
			if(count>0){
				list = advertServiceImpl.queryAdvertList(advertDTO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("pager", pager);
		}catch(Exception e){
			log.error("AdvertAction queryAdvertList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In AdvertAction queryAdvertList end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: queryAdvert 
	* @Description: 查询广告详情
	* @param   request
	* @param   response
	* @param   advertId
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/queryAdvertInfo.shtml")
	@ResponseBody
	public ResultMap queryAdvert(HttpServletRequest request,
			HttpServletResponse response,Integer advertId) {
		
		log.info("In AdvertAction queryAdvert Begin!");
		
		try{
			AdvertDTO dto = advertServiceImpl.queryAdvertInfo(advertId);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("dto", dto);
		}catch(Exception e){
			log.error("AdvertAction queryAdvert error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In AdvertAction queryAdvert end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: updateAdvert 
	* @Description: 修改广告
	* @param   request
	* @param   response
	* @param   advertDTO
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/updateAdvert.shtml")
	@ResponseBody
	public ResultMap updateAdvert(HttpServletRequest request,
			HttpServletResponse response,AdvertDTO advertDTO) {
		
		log.info("In AdvertAction updateAdvert Begin!");
		
		try{
			if(advertDTO.getId() == null){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			DTOdateSet.setUpdateInfo(advertDTO, request.getSession());
			advertServiceImpl.updateAdvert(advertDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("AdvertAction updateAdvert error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In AdvertAction updateAdvert end!");	
		
		return resultMap;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/queryAdvertRecommendList.shtml")
	@ResponseBody
	public ResultMap queryAdvertRecommendList(HttpServletRequest request,
			HttpServletResponse response,AdvertDTO advertDTO,Pager pager) {
		
		log.info("In AdvertAction queryAdvertList Begin!");
		
		try{
			advertDTO.setBegin(pager.getStartIndex());
			advertDTO.setPageSize(pager.getPageSize());
			
			int count = advertServiceImpl.queryAdvertListRecommendCount(advertDTO);
			pager.setTotalCount(count);
			
			List<AdvertDTO> list;
			if(count>0){
				list = advertServiceImpl.queryAdvertRecommendList(advertDTO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("pager", pager);
		}catch(Exception e){
			log.error("AdvertAction queryAdvertList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In AdvertAction queryAdvertList end!");	
		
		return resultMap;
	}
}
